﻿using System;
using System.Globalization;
using System.Web.UI.WebControls;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Practices.ObjectBuilder;
using WCSFContrib.ECommerceCatalog.BusinessEntities;

namespace WCSFContrib.ECommerceCatalog.User.Views
{
    public partial class Checkout : Microsoft.Practices.CompositeWeb.Web.UI.Page, ICheckoutView
    {
        private CheckoutPresenter _presenter;

        private int _selectedBillingAddressId;
        private int _selectedShippingAddressId;
        private int _selectedPaymentMethodId;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this._presenter.OnViewInitialized();
            }
            this._presenter.OnViewLoaded();
        }

        [CreateNew]
        public CheckoutPresenter Presenter
        {
            set
            {
                this._presenter = value;
                this._presenter.View = this;

                this._presenter.UserName = User.Identity.Name;
            }
        }

        protected void CompleteCheckoutButton_Click(object sender, EventArgs e)
        {
            string userName = User.Identity.Name;
            
            _selectedBillingAddressId = (int)BillingAddressGridView.DataKeys[BillingAddressGridView.SelectedIndex].Value;
            _selectedShippingAddressId = (int)ShippingAddressGridView.DataKeys[ShippingAddressGridView.SelectedIndex].Value;
            _selectedPaymentMethodId = (int)PaymentTypeGridView.DataKeys[PaymentTypeGridView.SelectedIndex].Value;

            _presenter.OnCompleteCheckout(userName);
        }

        protected void GoToProfileLinkButton_Click(object sender, EventArgs e)
        {
            _presenter.OnGoToUserProfilePage();
        }

        #region ICheckoutView Members

        public string CurrentUser
        {
            get { return UserTextbox.Text; }
            set { UserTextbox.Text = value; }
        }

        public DateTime OrderDate
        {
            get { return DateTime.Parse(OrderDateTextBox.Text); }
            set { OrderDateTextBox.Text = String.Format(CultureInfo.CurrentCulture, "{0}", value); }
        }

        public ICollection<Address> AddressList
        {
            set
            {
                BillingAddressContainerDataSource.DataSource = value;
                ShippingAddressContainerDataSource.DataSource = value;

                if (BillingAddressContainerDataSource.Items.Count > 0)
                {
                    BillingAddressGridView.SelectedIndex = 0;
                    ShippingAddressGridView.SelectedIndex = 0;
                }
                else
                    CompleteCheckoutButton.Enabled = false;
            }
        }

        public ICollection<Payment> PaymentsMethodsList
        {
            set
            { 
                PaymentsObjectContainerDataSource.DataSource = value;
                
                if (PaymentsObjectContainerDataSource.Items.Count > 0)
                    PaymentTypeGridView.SelectedIndex = 0;
                else
                    CompleteCheckoutButton.Enabled = false;
            }
        }

        public int BillingAddressId
        {
            get { return _selectedBillingAddressId; }
        }

        public int ShippingAddressId
        {
            get { return _selectedShippingAddressId; }
        }

        public int PaymentMethodId
        {
            get { return _selectedPaymentMethodId; }
        }

        #endregion
    }
}

